//
//  MqInfoPictureTableViewController.m
//  Meiqi
//
//  Created by 王郑 on 16/1/14.
//  Copyright © 2016年 KFC. All rights reserved.
//

#import "MqInfoPictureTableViewController.h"
#import "MqInfoPicureTableViewCell.h"
#import "MqInfoPictureModel.h"
#import "MqInfoPictureShowViewController.h"


@interface MqInfoPictureTableViewController ()


// 请求的起始位置
@property (assign, nonatomic) NSInteger requestStart;

// 数据源数组
@property (strong, nonatomic) NSMutableArray *listArray;

@end

@implementation MqInfoPictureTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.rowHeight = 300*kHeightBase;
    // 注册
    [self.tableView registerClass:[MqInfoPicureTableViewCell class] forCellReuseIdentifier:@"pictureCell"];
    
#pragma mark --- 数据源数组
    _listArray = [NSMutableArray array];
    _requestStart = 1;
#pragma mark --- 解析数据
    [self setUpData];
    
    
#pragma mark --- 上拉加载
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        _requestStart +=1;
        
        [self setUpData];
        
    }];
    
    
#pragma mark --- 下拉刷新
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
       
        _requestStart = 1;
        
        // 清空数据源数组
        
        [self setUpData];
        
    }];
    
}


- (void)setUpData
{
    [PKRequestManager requestWithUrlString:[NSString stringWithFormat:@"http://www.biketo.com/app.php?m=client&a=render&&classid=119&page=%ld&platform=1&pz=20&showType=pictureList&time=0&version=1",_requestStart] parDic:nil Method:GET finish:^(NSData *data) {
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSArray *list = dic[@"list"];
        
        if (_requestStart == 1) {
            
            [_listArray removeAllObjects];
            
        }
        
        
        for (NSDictionary *oneDic in list) {
            
            MqInfoPictureModel *model = [[MqInfoPictureModel alloc] init];
            [model setValuesForKeysWithDictionary:oneDic];
            [_listArray addObject:model];
            
        }
        
#pragma mark ----- 结束加载和刷新
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
   
        [self.tableView reloadData];
        
    } error:^(NSError *error) {
        
//        NSLog(@"%@",error);
        
    } path:@"pictureListData"];
    
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _listArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MqInfoPicureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pictureCell" forIndexPath:indexPath];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    // 展示数据
    MqInfoPictureModel *model = _listArray[indexPath.row];
    cell.model = model;
   
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MqInfoPictureModel *model = _listArray[indexPath.row];
    
   NSDictionary *dic = @{
                         @"id":model.picID,
                          @"title":model.title
                          };
  
    // 发送通知,要求主页面去跳转
    [[NSNotificationCenter defaultCenter]postNotificationName:@"picturePush" object:self userInfo:dic];
    
    
    
    
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
        //设置cell的显示动画为3D缩放
    
    //xy方向缩放的初始值为0.1
    
    cell.layer.transform = CATransform3DMakeScale(0.1, 0.1, 1);
    
    //设置动画时间为0.25秒,xy方向缩放的最终值为1
    [UIView animateWithDuration:0.5 animations:^{
        
        cell.layer.transform = CATransform3DMakeScale(1, 1, 1);
        
    }];
    
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
